package contest.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import contest.Contest;

/**
 * Servlet implementation class AddContestServlet
 */
public class AddContestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddContestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     */
    @SuppressWarnings("unchecked")
	private void addContest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	System.out.println(request.getContextPath() + " " + request.getSession().getServletContext().getRealPath(request.getRequestURI()));
    	System.out.println(request.getServletPath());
    	
    	
    	request.setCharacterEncoding("utf-8");
    	Contest contest = new Contest();
    	String title = request.getParameter("title");
    	if(title == null) {
    		return;
    	}
    	String time = request.getParameter("time");
    	if(time == null) {
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    		Date currentDate = new Date();
    		time = formatter.format(currentDate);
    	}
    	String source = request.getParameter("source");
    	String article = request.getParameter("article");
    	
    	contest.setTitle(title);
    	contest.setTime(time);
    	contest.setSource(source);
    	System.out.println("title="+title+" time="+time+" source="+source+" article="+article);
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.addContest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.addContest(request, response);
	}

}
